from PySFML import sf
from animation import animation_single
import camera
import image

class map (object) :
    """ Map object holds the clickmap information to handle clicks on the
        world; also draws the minimap"""
    def __init__ (self, tower, (world_x, world_y)) :
        """ Initialize the map object."""
        self.camera = camera.camera ()
        self.click_map = {}
        self.world_prop = 0.0, 0.0
        self.world_prop = 200.0 / float (world_x)
        self.world_x = world_x
        self.aspect = (float (world_y) / float(world_x))
        self.x = 200
        self.y = self.aspect * 200.0
        levels = tower.get_levels ()
        self.level_height = (self.y / levels)
        self.mini_map = sf.Image (self.x, self.y, sf.Color (0,0,0,0))
        self.mini_map_sprite = animation_single (self.mini_map)
        self.mini_map_sprite.set_position (5, 65)
        self.draw_map ()
        self.tower = tower
        self.tower.map = self
        cam_x, cam_y = self.camera.get_cam ()
        self.cambox_x = cam_x * self.world_prop
        self.cambox_y = cam_y * self.world_prop
        self.ground = animation_single (image.load ("mini_ground.png"))
        self.sky = animation_single (image.load ("mini_back.png"))
        self.ground_size = (10 * self.level_height)
        self.ground.set_position (5, 65 + (self.y - self.ground_size))
        self.sky.set_position (5, 65)
        self.ground.Resize (self.x, self.ground_size)
        self.sky.Resize (self.x, self.y - self.ground_size)
        self.mini_border = sf.Shape.Rectangle (5, 65, self.x + 5, self.y + 65, sf.Color (0,0,0,0), 2, sf.Color (0,0,0))
        self.cambox = sf.Shape.Rectangle (0, 0, self.cambox_x, self.cambox_y, sf.Color (0,0,0,0), 2, sf.Color (255,255,255))
    def draw_map (self) :
        """ Generate the minimap image """
        grey = sf.Color (80, 80, 80, 255)
        for level in self.click_map :
            for x in range (self.world_x) :
                if not self.click_map[level][x] == None :
                    x_pixel = x * self.world_prop
                    y_pixel =  self.y - (level * self.level_height) - self.ground_size
                    for i in range (self.level_height + 1) :
                        self.mini_map.SetPixel (x_pixel, y_pixel + i, grey)
    def set_callback (self, level, x, x2, object) :
        """ Set a callback object in the click map """
        world_x, world_y = self.camera.get_world ()
        if not level in self.click_map :
            self.click_map[level] = [None for y in range (world_x)]
        for i in range (x, x2) :
            self.click_map[level][i] = object
    def world_click (self, x, y, cargo_2) :
        """ Return the object at a certain position in the clickmap given
            the world coords of the mouse"""
        level = (y / 36) + 1
        try :
            return self.click_map[level][x]
        except :
            return None
    def click (self, x, y) :
        """ Handle the minimap being clicked """
        self.camera.center (((x - 5)/self.world_prop), ((y - 65)/self.world_prop))
    def resize (self, x, y) :
        """ Resize the camera rectangle to be drawn on the minimap """
        self.cambox_x = x * self.world_prop
        self.cambox_y = y * self.world_prop
    def draw_minimap (self) :
        """ Draw the minimap to the screen """
        pos_x, pos_y = self.camera.get_cam_pos ()
        self.camera.draw_sprite_ignore (self.sky)
        self.camera.draw_sprite_ignore (self.ground)
        self.camera.draw_shape_ignore (self.mini_border)
        self.cambox.SetPosition (5 + (pos_x * self.world_prop), 65 + (pos_y * self.world_prop))
        self.camera.draw_sprite_ignore (self.mini_map_sprite)
        self.camera.draw_shape_ignore (self.cambox)